<template>
  <div class>
    <el-card class="btns">
      <el-button size="medium" type="primary" @click="submitForm('ruleForm')">查询</el-button>
      <el-button size="medium" @click="resetForm('ruleForm')">重置</el-button>
      <el-button size="medium" type="primary" icon="el-icon-plus" @click="showDialog">添加</el-button>
    </el-card>
    <el-card style="position:relative;">
      <el-form
        :style="style"
        :model="ruleForm"
        ref="ruleForm"
        inline
        label-width="100px"
        class="demo-ruleForm">
        <div class="icon-arrow" v-if="folding">
          <i @click="offForm=!offForm"
            :title="offForm?'展开表单':'收起表单'"
            :class="offForm?'el-icon-arrow-down':'el-icon-arrow-up'"></i>
        </div>
	                	<el-form-item label="消息类型">
		                	<el-input v-model="ruleForm.typeCode"></el-input>
		              </el-form-item>
      </el-form>
    </el-card>
    <el-table stripe :data="tableData" border style="width: 100%">
    		<el-table-column type="index" width="60px" align="center" fixed="left" label="序号"></el-table-column>
                    	<el-table-column align="center" label="消息类型代码">
	                <template slot-scope="scope">
	                     	<span>{{scope.row.typeCode}}</span>
	                </template>
	            		</el-table-column>
                    	<el-table-column align="center" label="消息类型名称">
	                <template slot-scope="scope">
	                     	<span>{{scope.row.typeName}}</span>
	                </template>
	            		</el-table-column>
                  <el-table-column align="center" label="消息类型描述">
	                <template slot-scope="scope">
	                     	<span>{{scope.row.remark}}</span>
	                </template>
	            		</el-table-column>
       <el-table-column label="操作" width="180px" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="showEditDialog(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-card style="text-align:right;">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="currentPage"
        :page-sizes="[ 10, 20,30, 40]"
        :page-size="pageSize"
        background
        layout="total, sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- --------------------------------------------------------添加弹出框 -->
    <el-dialog title="新增消息类型" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="form" ref="addForm" :rules="rules">
                	<el-form-item label="消息类型代码" :label-width="formLabelWidth" prop="typeCode">
                 	<el-input v-model="form.typeCode" autocomplete="off"></el-input>
	             </el-form-item>
                	<el-form-item label="消息类型名称" :label-width="formLabelWidth" prop="typeName">
                 	<el-input v-model="form.typeName" autocomplete="off"></el-input>
	             </el-form-item>
               <el-form-item label="消息类型描述" :label-width="formLabelWidth">
                 	<el-input v-model="form.remark" autocomplete="off"></el-input>
	             </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- --------------------------------------------------------编辑弹出框 -->
    <el-dialog title="编辑消息类型" :visible.sync="dialogEditVisible" :close-on-click-modal="false">
      <el-form :model="editForm" ref="editForm" :rules="rules">
        <el-form-item label="消息类型代码" :label-width="formLabelWidth" prop="typeCode">
          <el-input v-model="editForm.typeCode" @input="change($event)" autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item label="消息类型名称" :label-width="formLabelWidth" prop="typeName">
        <el-input v-model="editForm.typeName" @input="change($event)" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="消息类型描述" :label-width="formLabelWidth">
          <el-input v-model="editForm.remark" @input="change($event)" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      dialogFormVisible: false,
      dialogEditVisible: false,
      offForm: true, //是否折叠
      folding: false, //表单是否超过一行
      formLabelWidth: "100px",
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      ruleForm: {
             			typeCode:'',
             			typeName:'',
      },
      style: {},
      rules: {
       
      },
      form: {
             			typeCode:'',
             			typeName:'',
      },
      editForm: {
             	id:'',
             	typeCode:'',
             	typeName:'',
      }
    };
  },

  mounted() {
    this.init();
    let formHeight = this.$refs.ruleForm.$el.clientHeight;
    if (formHeight > 62) {
      this.folding = true;
      this.style = {
        height: "62px",
        overflow: "hidden"
      };
    } else {
      this.folding = false;
      this.style = {
        height: "auto",
        overflow: "hidden"
      };
    }
  },
  computed: {
  },
  watch: {
    offForm: function(v) {
      if (v) {
        this.style = {
          height: "62px",
          overflow: "hidden"
        };
      } else {
        this.style = {
          height: "auto",
          overflow: "hidden"
        };
      }
    }
  },
  methods: {
    change(v){
      this.$forceUpdate();
    },
    sizeChange(n) {
      this.pageSize = n;
      this.init();
    },
    currentChange(n) {
      this.currentPage = n;
      this.init();
    },
    init() {
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.ruleForm
      };
      this.$api
        .formPost( "/api/mess/type/doGetList", params)
        .then(res => {
          if (res.retHead.code == "0000000") {
            this.tableData = res.retBody.list;
            this.total = res.retBody.total;
            this.currentPage = res.retBody.pageNum;
          }
        });
    },
    showDialog() {
      this.dialogFormVisible = true;
      this.form ={};
    },
    showEditDialog(index, row) {
      this.editForm = {};
      this.editForm.id=row.id;
      this.editForm.typeCode=row.typeCode;
      this.editForm.typeName=row.typeName;
      this.editForm.remark=row.remark;
      this.dialogEditVisible = true;
    },
    handleAdd() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.$api
            .formPost( "/api/mess/type/doSave", this.form)
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "添加成功！",
                  type: "success"
                });
                this.dialogFormVisible = false;
                this.init();
              } else {
                this.$message({
                  message: res.retHead.message,
                  type: "error"
                });
              }
            });
        }
      });
    },
    handleEdit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          let params = this.editForm;
          this.$api
            .formPost( "/api/mess/type/doModById", params)
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message.success("执行成功！");
                this.init();
                this.dialogEditVisible = false;
              } else {
                this.$message.error(res.retHead.message);
              }
            });
        }
      });
    },

    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$api
            .formPost( "/api/mess/type/doRmById", {
              id: row.id
            })
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "操作成功！",
                  type: "success"
                });
                this.init();
              } else {
                this.$message({
                  message: "操作失败！",
                  type: "error"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    submitForm(formName) {
      this.init();
    },
    resetForm(formName) {
      // this.$refs[formName].resetFields();
      this.ruleForm = {};
    }
  },
  created() {}
};
</script>
<style lang='scss' scoped>
.btns {
  text-align: right;
}
.icon-arrow {
  position: absolute;
  bottom: 5px;
  left: 0;
  width: 100%;
  height: 1px;
  background-color: #f1f1f1;
  i {
    background-color: #fff;
    position: absolute;
    color: #999;
    bottom: -7px;
    margin-left: 50%;
    transform: translateX(-50%);
    cursor: pointer;
  }
}
</style>